package com.itheima.controller;

import com.itheima.pojo.People;
import com.itheima.pojo.Result;
import com.itheima.service.PeopleService;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/people")
@RestController
public class PeopleController {
    @Autowired
    private PeopleService peopleService;

    @RequestMapping("/personalInformation")
    public Result getPersonalInformation(HttpServletRequest request) {
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        System.out.println("claims:"+claims);

        Integer id = Integer.parseInt(claims.get("id").toString());
        System.out.println("id:"+id);
        People people = new People();
        people.setId(id);
        return Result.success(peopleService.getPersonalInformation(people));
    }
    @RequestMapping("/findAllPeople")
    public Result getPeopleList() {

        return Result.success(peopleService.getPeopleList());
    }
    @RequestMapping("/findPeople")
    public Result searchPeople(@RequestBody People people) {
        return Result.success(peopleService.searchPeople(people));
    }
    @RequestMapping("/changePassword")
    public Result changePassword(@RequestBody People people) {
        Integer id = people.getId();
        String oldPassword = people.getPassword();
        String newPassword = people.getAccount();

        return peopleService.changePassword(oldPassword,newPassword,id);
    }
    @RequestMapping("/changePersonInformation")
    public Result changePersonalInformation(@RequestBody People people) {
        peopleService.changePersonalInformation(people);
        return Result.success();
    }
    @RequestMapping("/setPhone")
    public Result setPhone(@RequestBody People people){
        peopleService.setPhone(people);
        return Result.success();
    }
    @RequestMapping("/countHouduan")
    public Result countHouduan(){
        return Result.success(peopleService.countHouduan());
    }
    @RequestMapping("/countQianduan")
    public Result countQianduan(){
        return Result.success(peopleService.countQianduan());
    }
    @RequestMapping("/countQianrushi")
    public Result countQianrushi(){
        return Result.success(peopleService.countQianrushi());
    }
}
